package com.renfei.multimediatask.ui.task11.filter;

import android.content.Context;
import android.graphics.Bitmap;
import android.opengl.GLES20;
import android.util.Log;

import com.renfei.multimediatask.R;
import com.renfei.multimediatask.util.OpenGlUtils;
import com.renfei.multimediatask.util.glutils.Rotation;
import com.renfei.multimediatask.util.glutils.TextureRotationUtil;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;

public class BlackWhiteFboRender {

    private static final String TAG = "TextWaterMarkFboRender";
    private Context context;


    private static final int FLOAT_SIZE = 4;//一个float数据，占几个字节 一个字节有8位
    private static final int POINT_VEC = 2;//坐标有两个向量 在平面中 只有x,y

    public static final float CUBE[] = {
            -1.0f, -1.0f,
            1.0f, -1.0f,
            -1.0f, 1.0f,
            1.0f, 1.0f,
    };
    private FloatBuffer vertexFBOBuffer;
    private FloatBuffer texturFBOeBuffer;

    private int program;
    private int v_Position;//attribute vec4
    private int f_Position;//attribute vec2
    private int sTexture;//uniform
//    private int textureTransform;//矩阵
//    protected float[] mTextureTransformMatrix = new float[16];


    //水印
    public static final float[] waterMark_vertexData = {
            0.0f, 0.0f,
            0.0f, 0.0f,
            0.0f, 0.0f,
            0.0f, 0.0f
    };

    //这里使用的是屏幕坐标，  这里与FBO显示 的不统一 ，
          public static final float[] waterMark_textureData = {
                  0.0f, 1.0f,
                  1.0f, 1.0f,
                  0.0f, 0.0f,
                  1.0f, 0.0f
          };

   //   public static final float[] waterMark_textureData = {
   //           0.0f, 0.0f,
   //           1.0f, 0.0f,
   //           0.0f, 1.0f,
   //           1.0f, 1.0f
   //   };

    private FloatBuffer waterMarkVertexBuffer;
    private FloatBuffer waterMarkTextureBuffer;


    private Bitmap bitmap;
    private int bitmapTextureID;


    public BlackWhiteFboRender(Context context) {
        this.context = context;


        vertexFBOBuffer = ByteBuffer.allocateDirect(CUBE.length * 4)
                .order(ByteOrder.nativeOrder())
                .asFloatBuffer();
        vertexFBOBuffer.put(CUBE).position(0);


        float[] flipTexture = TextureRotationUtil.getRotation(Rotation.NORMAL, false, true);

        texturFBOeBuffer = ByteBuffer.allocateDirect(flipTexture.length * 4)
                .order(ByteOrder.nativeOrder())
                .asFloatBuffer();
        texturFBOeBuffer.put(flipTexture).position(0);

        initShuiYinImage();
    }


    public void onCreate() {
        //这两句代码， 开启混合
        //我们在绘制透明水印图片时，需要这两句，开启图片透明度 ，否则图片透明背景变成了黑色背景

        GLES20.glEnable(GLES20.GL_BLEND);
        GLES20.glBlendFunc(GLES20.GL_SRC_ALPHA,GLES20.GL_ONE_MINUS_SRC_ALPHA);

        String vertexSource = OpenGlUtils.readShaderFromRawResource(context, R.raw.camera_preview_black_white_fbo_vertex_shader);
        String fragmentSource = OpenGlUtils.readShaderFromRawResource(context, R.raw.camera_preview_black_white_fbo_fragment_shader);

        program = OpenGlUtils.createProgram(vertexSource, fragmentSource);

        v_Position = GLES20.glGetAttribLocation(program, "v_Position");
        f_Position = GLES20.glGetAttribLocation(program, "f_Position");
        sTexture = GLES20.glGetUniformLocation(program, "sTexture");
//        textureTransform=GLES20.glGetUniformLocation(program,"textureTransform");
        //创建水印纹理ID
        bitmapTextureID = OpenGlUtils.loadBitmapTexture(bitmap);
    }

    public void onChange(int width, int height) {
        GLES20.glViewport(0, 0, width, height);
    }

//    public void setTextureTransformMatrix(float[] mtx) {
//        mTextureTransformMatrix = mtx;
//    }


    public void onDraw(final int textureId) {
        GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
        GLES20.glClearColor(0.1f, 0.4f, 0.7f, 1.0f);
        GLES20.glUseProgram(program);


//        GLES20.glUniformMatrix4fv(textureTransform,1,false,mTextureTransformMatrix,0);
        //使用纹理
        GLES20.glUniform1i(sTexture, 0);

        GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureId);
        GLES20.glEnableVertexAttribArray(v_Position);
        GLES20.glVertexAttribPointer(v_Position, 2, GLES20.GL_FLOAT, false, 0, vertexFBOBuffer);
        GLES20.glEnableVertexAttribArray(f_Position);
        GLES20.glVertexAttribPointer(f_Position, 2, GLES20.GL_FLOAT, false, 0, texturFBOeBuffer);
        GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4);
        GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, 0);


        //绘制水印
        GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, bitmapTextureID);
        GLES20.glEnableVertexAttribArray(v_Position);
        GLES20.glVertexAttribPointer(v_Position, 2, GLES20.GL_FLOAT, false, 0, waterMarkVertexBuffer);
        GLES20.glEnableVertexAttribArray(f_Position);
        GLES20.glVertexAttribPointer(f_Position, 2, GLES20.GL_FLOAT, false, 0, waterMarkTextureBuffer);
        GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4);
        GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, 0);

    }


    private void initShuiYinImage() {
        /**
         * 获取水印图片
         */
        bitmap = OpenGlUtils.createTextImage("任飞666", 50,
                "#ff0000", "#00000000", 0);
        //计算水印长宽比，坐标点
        float ratio = 1.0f * bitmap.getWidth() / bitmap.getHeight();

        float w = ratio * 0.1f;//实际绘制的高


        Log.e(TAG, "水印图片 : w    " + w);

        //左下
        waterMark_vertexData[0] = -0.8f;
        waterMark_vertexData[1] = -0.8f;
        //右下
        waterMark_vertexData[2] = -0.8f + w;
        waterMark_vertexData[3] = -0.8f;

        //左上
        waterMark_vertexData[4] = -0.8f;
        waterMark_vertexData[5] = -0.7f;
        //右上
        waterMark_vertexData[6] = -0.8f + w;
        waterMark_vertexData[7] = -0.7f;

        waterMarkVertexBuffer = ByteBuffer.allocateDirect(waterMark_vertexData.length * 4)
                .order(ByteOrder.nativeOrder())
                .asFloatBuffer();
        waterMarkVertexBuffer.put(waterMark_vertexData).position(0);

        waterMarkTextureBuffer = ByteBuffer.allocateDirect(waterMark_textureData.length * 4)
                .order(ByteOrder.nativeOrder())
                .asFloatBuffer();
        waterMarkTextureBuffer.put(waterMark_textureData).position(0);
    }
}
